/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apt.professionaltag;

import com.apt.bean.Professional;
import com.apt.tag.Process_Page;
import java.util.HashMap;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 *
 * @author Bach
 */
public class ProfessionnalTagHandler extends SimpleTagSupport {

    private String page;

    /**
     * Called by the container to invoke this tag. The implementation of this
     * method is provided by the tag library developer, and handles all tag
     * processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();
        int cursor1;
        HashMap<Integer, Professional> hm = GetProfessional.getListProfessional();
        try {
            if (hm != null && hm.size() != 0) {
                out.println(Process_Page.divPage(page, hm.size(), 14));
                int uid_f = 1;
                int uid_l = 30;

                try {
                    cursor1 = Integer.parseInt(page);

                } catch (NumberFormatException ex) {
                    cursor1 = 1;
                }
                if (cursor1 > 1) {
                    uid_f = (cursor1 - 1) * 30;
                    uid_l = cursor1 * 30;
                }
                int current_page = uid_l / 30;
                for (int i = uid_f; i < uid_l; i++) {
                    Professional u = hm.get(i);
                    if (u != null) {
                        int status = Integer.parseInt(u.getProfessional_Status());
                        String casteid = u.getProfessional_Id();
                        StringBuilder sb = new StringBuilder(" <tr>");
                        // sb.append(" <td><input type='checkbox' /></td>");
                        if (status == 1) {
                            sb.append(" <td><form name=\"frm" + casteid + "\" action=\"UpdateProfessionalServlet\" method=\"get\">\n"
                                    + "<input type=\"text\" name= ct" + casteid + "  value=\"" + u.getProfessinal_Name() + "\"/> "
                                    + "<input name=\"id\" type=\"hidden\" value=\"" + casteid + "\"/>"
                                    + "<input name=\"pageno\" type=\"hidden\" value=\"" + page + "\"/>"
                                    + "<input type=\"submit\"  id=\"\" value=\"Update\" />\n"
                                    + "</form></td>");
                            sb.append("  <td><a href=\"#\" title=\"title\">Enable</a></td>");
                        } else {
                            sb.append("<td>" + u.getProfessinal_Name() + "</td>");
                            sb.append("  <td><a href=\"#\" title=\"title\">Disable</a></td>");
                        }
                        sb.append("    <td>");
                        sb.append("        <!-- Icons -->");
                        if (status == 1) {
                            sb.append("<a href=\"DisableProfessionaltServlet?pageno=" + current_page + "&professname=" + u.getProfessinal_Name() + "\" title=\"Disable\"><img src=\"resources/images/icons/cross.png\" alt=\"Disable\" /></a>");
                        } else {
                            sb.append("<a href=\"EnableProfessionaltServlet?pageno=" + current_page + "&professname=" + u.getProfessinal_Name() + "\" title=\"Enable\"><img src=\"resources/images/icons/pencil.png\" alt=\"Enable\" /></a>");
                        }
//                        sb.append(" <a href=\"Navigation?menu=17&pageno=" + current_page +"&payname="+u.getPayment_Type_Name()+ "\" title=\"Enable\" ><img src=\"resources/images/icons/view.png\" alt=\"Enable\" /></a>");
                        sb.append("   </td>");
                        sb.append(" </tr>");

                        out.print(sb.toString());

                    }
                }
            }
            JspFragment f = getJspBody();
            if (f != null) {
                f.invoke(out);
            }

        } catch (java.io.IOException ex) {
            throw new JspException("Error in ProfessionnalTagHandler tag", ex);
        }
    }

    public void setPage(String page) {
        this.page = page;
    }
}
